package com.eascs.ticket.ticket.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.common.form.IdForm;
import com.eascs.ticket.common.form.OptionalIdForm;
import com.eascs.ticket.ticket.entity.TicketReleaseNote;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteDelete;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteUpdate;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteView;
import com.eascs.ticket.ticket.form.TicketIdForm;
import com.eascs.ticket.ticket.form.TicketReleaseNoteForm;
import com.eascs.ticket.ticket.form.TicketSearchForm;
import com.eascs.ticket.ticket.service.TicketReleaseNoteService;
import com.eascs.ticket.ticket.service.TicketService;
import com.eascs.ticket.ticket.vo.OrderByConstant;

@RestController
public class TicketReleaseNoteRestController extends BaseRestController {

	@Inject
	private TicketReleaseNoteService ticketReleaseNoteService;

	@Inject
	private TicketSearchComponent ticketSearchComponent;

	@Inject
	private TicketService ticketService;

	@RequestMapping(path = "/ticket/releaseNote/getTicketReleaseNoteInsertView")
	public HtmlRestResult getTicketReleaseNoteEditView(TicketIdForm ticketIdForm) {
		TicketReleaseNote ticketReleaseNote = new TicketReleaseNote();
		ticketReleaseNote.setTicketId(ticketIdForm.getTicketId());
		ViewContext.put("ticketReleaseNote", ticketReleaseNote);
		return HtmlRestResult.popHtmlFragment("ticket/opt/getTicketReleaseNoteEditView");
	}

	@RequestMapping(path = "/ticket/releaseNote/getTicketReleaseNoteUpdateView")
	public HtmlRestResult getTicketReleaseNoteEditView(OptionalIdForm optionalIdForm) {
		if (StringUtil.hasText(optionalIdForm.getId())) {
			ViewContext.put("ticketReleaseNote", ticketReleaseNoteService.getTicketReleaseNoteById(optionalIdForm.getId()));
		}
		return HtmlRestResult.popHtmlFragment("ticket/opt/getTicketReleaseNoteEditView");
	}

	@RequestMapping(path = "/ticket/releaseNote/getTicketReleaseNoteView")
	public HtmlRestResult getTicketReleaseNoteView(TicketIdForm ticketIdForm) {
		TicketReleaseNoteView ticketReleaseNoteView = this.ticketReleaseNoteService.getValidTicketReleaseNoteViewByTicketId(ticketIdForm.getTicketId());
		ViewContext.put("ticketReleaseNoteView", ticketReleaseNoteView);
		ViewContext.put("ticketId", ticketIdForm.getTicketId());
		return HtmlRestResult.html("ticket/view/getTicketReleaseNoteView");
	}

	@RequestMapping(path = "/ticket/releaseNote/saveTicketReleaseNote")
	public BaseRestResult<String> saveTicketReleaseNote(TicketReleaseNoteForm ticketReleaseNoteForm) {
		if (StringUtil.hasText(ticketReleaseNoteForm.getId())) {
			TicketReleaseNoteUpdate ticketReleaseNoteUpdate = new TicketReleaseNoteUpdate();
			ObjectUtil.copySimpleProperties(ticketReleaseNoteForm, ticketReleaseNoteUpdate);
			EntityUtil.initUpdateEntity(ticketReleaseNoteUpdate);
			ticketReleaseNoteService.updateTicketReleaseNoteById(ticketReleaseNoteUpdate);
		} else {
			TicketReleaseNote ticketReleaseNote = new TicketReleaseNote();
			ObjectUtil.copySimpleProperties(ticketReleaseNoteForm, ticketReleaseNote);
			EntityUtil.initInsertEntity(ticketReleaseNote);
			ticketReleaseNote.setStatus(StatusConstant.STATUS_OK);
			ticketReleaseNoteService.insertTicketReleaseNoteById(ticketReleaseNote);
		}
		return BaseRestResult.refresh();
	}

	@RequestMapping(path = "/ticket/releaseNote/deleteTicketReleaseNote")
	public BaseRestResult<String> deleteTicketReleaseNote(IdForm idForm) {
		TicketReleaseNoteDelete ticketReleaseNoteDelete = new TicketReleaseNoteDelete();
		ticketReleaseNoteDelete.setId(idForm.getId());
		EntityUtil.initUpdateEntity(ticketReleaseNoteDelete);
		ticketReleaseNoteDelete.setStatus(StatusConstant.STATUS_DEL);
		ticketReleaseNoteService.deleteTicketReleaseNoteById(ticketReleaseNoteDelete);
		return BaseRestResult.refresh();
	}

	@RequestMapping(path = "/ticket/releaseNote/findTicket")
	public PaginationRestResult findTicket(TicketSearchForm ticketSearchForm) {
		if (StringUtil.isEmpty(ticketSearchForm.getOrderBy())) {
			ticketSearchForm.setOrderBy(OrderByConstant.ORDER_BY_VERSION);
		}
		PageResult<Object[]> pageResult = ticketSearchComponent.searchTicket(ticketSearchForm, "sql.TicketReleaseNoteRestController.findTicket");
		PaginationInfo paginationInfo = ticketSearchComponent.buildPaginationInfo(ticketSearchForm, pageResult);
		paginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxButton("查看", "/ticket/releaseNote/viewTicketReleaseNote?ticketId=") });
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@RequestMapping(path = "/ticket/releaseNote/viewTicketReleaseNote")
	public HtmlRestResult viewTicketReleaseNote(TicketIdForm ticketIdForm) {
		TicketReleaseNoteView ticketReleaseNoteView = this.ticketReleaseNoteService.getValidTicketReleaseNoteViewByTicketId(ticketIdForm.getTicketId());
		ViewContext.put("ticketReleaseNoteView", ticketReleaseNoteView);
		ViewContext.put("ticket", ticketService.getTicketById(ticketIdForm.getTicketId()));
		return HtmlRestResult.popHtmlFragment("ticket/view/viewTicketReleaseNote");
	}
}
